package penguin.masking;

import java.io.*;
import java.util.Properties;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class ContextConfigLoad {

    private Properties properties;

    private static ContextConfigLoad config = null;

    private ContextConfigLoad() {

    }

    public static ContextConfigLoad configLoad() {
        if (config != null) {
            return config;
        }
        ContextConfigLoad configLoad = new ContextConfigLoad();
        configLoad.loadProperties();
        config = configLoad;
        return config;
    }

    public static void clear() {
        config = null;
    }

    private void loadProperties() {
        properties = new Properties();
        String resource = ContextConfigLoad.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String fileName = "data-masking-config.properties";
        File file = new File(new File(resource).getParentFile(), fileName);
        try (InputStream ips = file == null || !file.exists() ? ContextConfigLoad.class.getResourceAsStream("/" + fileName) : new FileInputStream(file);
             BufferedReader ipss = new BufferedReader(new InputStreamReader(ips))) {
            properties.load(ipss);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(String key, String value) {
        properties.setProperty(key, value);
    }
}
